from pptx import Presentation
from pptx.chart.data import CategoryChartData, ChartData

def page1(slide, title, date):
    print('processing page1')
    shape1 = slide.shapes[0]
    shape2 = slide.shapes[1]
    p1 = shape1.text_frame.paragraphs[0]
    p1.runs[0].text = title
    shape2.text_frame.paragraphs[0].runs[1].text = date

def page2(slide, total_text, summary_text, navs):
    total = slide.shapes[2]
    summary = slide.shapes[6]
    total.text_frame.paragraphs[0].runs[0].text = total_text
    summary.text_frame.paragraphs[0].runs[0].text = summary_text

    #资产净值表格
    table = slide.shapes[3].table
    for i in range(1, 7):
        nav = navs[i-1]
        table.cell(i, 0).text = nav[0]
        table.cell(i, 1).text = str(nav[1])
        table.cell(i, 2).text = str(nav[2])  

    #资产净值的折线图
    chart = slide.shapes[5].chart
    #准备数据
    cats = []
    series = []
    for nav in navs:
        cats.append(nav[0])
        series.append(str(nav[1]))

    chart_data = ChartData()
    chart_data.categories = cats
    chart_data.add_series('净值', series)
    chart.replace_data(chart_data)


def page3(slide, holdings, alloc):
    table = slide.shapes[1].table
    for i in range(1, 11):
        hld = holdings[i-1]
        table.cell(i, 0).text = hld[0]
        table.cell(i, 1).text = hld[1]
        table.cell(i, 2).text = str(hld[2])

    #资产类别表格
    table = slide.shapes[4].table
    for i in range(1, 5):
        alc = alloc[i-1]
        table.cell(i, 0).text = alc[0]
        table.cell(i, 1).text = str(alc[1])

    #资产类别饼图
    chart = slide.shapes[5].chart
    chart_data = ChartData()
    categories = []
    series = []
    for alc in alloc:
        categories.append(alc[0])
        series.append(alc[1])

    chart_data.categories = categories
    chart_data.add_series('Series 1', series)
    chart.replace_data(chart_data)


def page4(slide, comment):
    shape = slide.shapes[1]
    tf = shape.text_frame
    tf.text = comment


def one_ppt(client):
    ppt = Presentation('客户报表.pptx')
    slides = ppt.slides

    client_name = client['name']
    report_date = client['date']
    page1(slides[0], client_name, report_date)

    total = client['total']
    summary = client['summary']
    navs = client['navs']
    page2(slides[1], total, summary, navs)

    holdings = client['holdings']
    alloc = client['alloc']
    page3(slides[2], holdings, alloc)

    comment = client['comment']
    page4(slides[3], comment)
    ppt.save(f'{client_name}.pptx')

import mydata
mydata.load('data.xlsx')
for client in mydata.client_list:
    one_ppt(client)
